package com.speedata.xuyanshuai;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemProperties;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.speedata.uhftest.R;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * @author xuyan  刘锟调试
 */
public class FirstbootActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resulttest2);
        initView();
        intentFilter();
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }

    private void intentFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("nlscan.action.SCANNER_RESULT");
        registerReceiver(receiver, filter);
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        //bt2 = findViewById(R.id.bt2);

        bt1.setOnClickListener(this);
        //bt2.setOnClickListener(this);

        et = findViewById(R.id.et);


//        String s = "()";
//        byte[] s2;
//
//        Log.d("byte", String.valueOf(s.getBytes()));
//        Toast.makeText(this, String.valueOf(s.getBytes()), Toast.LENGTH_SHORT).show();
//        List<byte[]> a = new ArrayList<>();

        // startActivity(new Intent("com.spd.action.SCAN.SERVICE"));
//        startActivity(new Intent(ResultTestActivity.this, Sp60TestActivity.class));
//        finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
//                //open("on");
//                //startActivity(new Intent(ResultTestActivity.this, FlexboxTextActivity.class));
//                Intent intent = new Intent("com.se4500.onDecodeComplete");
//                intent.putExtra("se4500", "sendstr");
//                sendBroadcast(intent);
//                break;
            case R.id.bt2:
                //open("off");
                //startActivity(new Intent(FirstbootActivity.this, MainActivity.class));
                int datetimertc = SystemProperties.getInt("persist.sys.firstboot.rtc", -1);

                if(datetimertc == 1){
                    Log.e("TAG", "persist.sys.firstboot.rtc is 1###");

                    Handler handler = new Handler();

                    handler.postDelayed(() -> {
                        execCmd("date 123012002018.59 set");
                        execCmd("busybox hwclock -w");
                    }, 0);
                    //    execCmd("date 123012002018.59 set");
                    //     execCmd("busybox hwclock -w");
                    SystemProperties.set("persist.sys.firstboot.rtc","0");
                }
                break;
            default:
                break;
        }
    }

    private static BufferedWriter TorchFileWrite;

    /**
     * aw2013,
     *
     * @param str "on"  "off"
     */
    public static void open(String str) {
        File TorchFileName = new File("/sys/class/misc/aw2013/led2");
        try {
            TorchFileWrite = new BufferedWriter(new FileWriter(TorchFileName, false));
            TorchFileWrite.write(str);
            TorchFileWrite.flush();
            TorchFileWrite.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 广播接收器
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @SuppressLint("SetTextI18n")
        @Override
        public void onReceive(Context context, Intent intent) {

            switch (Objects.requireNonNull(intent.getAction())) {
                case "nlscan.action.SCANNER_RESULT":

                    String svalue1 = intent.getStringExtra("SCAN_BARCODE1");
                    int id = intent.getIntExtra("SCAN_BARCODE_TYPE", -1);
                    et.setText(svalue1);
                    bt1.setText(id + "");
                default:
                    break;
            }

        }
    };

    private void execCmd(String cmd) {
        try {
            Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
